package com.lanou.moving.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lanou.moving.R;
import com.lanou.moving.entity.SingerMV;
import com.lanou.moving.utils.Exchange;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by dllo on 15/10/24.
 */
public class SingerMVAdapter extends BaseAdapter {
    private Context context;
    private List<SingerMV.DataEntity> mvs;
    private SingerMV.DataEntity mv;

    public SingerMVAdapter(Context context, List<SingerMV.DataEntity> mvs) {
        this.context = context;
        this.mvs = mvs;
    }

    @Override
    public int getCount() {
        return mvs != null && mvs.size() > 0 ? mvs.size() : 0;
    }

    @Override
    public SingerMV.DataEntity getItem(int position) {
        return mvs.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.singer_mv_item, null);
            holder = new ViewHolder(convertView);
            holder.mv_linear = (LinearLayout) convertView.findViewById(R.id.mv_linear);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        mv = getItem(position);
        if (mv != null) {
            String play = Exchange.getCount(mv.getPickCount());
            String comment = Exchange.getCount(mv.getBulletCount());
            if (mv.getPicUrl() != null) {
                Picasso.with(context).load(mv.getPicUrl()).placeholder(R.mipmap.img_mv_default_image)
                        .error(R.mipmap.img_mv_default_image).into(holder.mvItemPicture);
            }else if (mv.getMvList().size() > 0){
                Picasso.with(context).load(mv.getMvList().get(0).getPicUrl()).placeholder(R.mipmap.img_mv_default_image)
                        .error(R.mipmap.img_mv_default_image).into(holder.mvItemPicture);
            }
            holder.mvItemName.setText(mv.getVideoName());
            holder.mvItemSinger.setText(mv.getSingerName());
            holder.singerMvPlay.setText(play);
            holder.singerMvComment.setText(comment);
            holder.mv_linear.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                }
            });
        }

        return convertView;
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'singer_mv_item.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder {
        @Bind(R.id.mv_item_picture)
        ImageView mvItemPicture;
        @Bind(R.id.mv_item_name)
        TextView mvItemName;
        @Bind(R.id.mv_item_singer)
        TextView mvItemSinger;
        @Bind(R.id.singer_mv_play)
        TextView singerMvPlay;
        @Bind(R.id.singer_mv_comment)
        TextView singerMvComment;
        private LinearLayout mv_linear;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
